package com.boot.learning.config;

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Springfox：swagger-springmvc结合后的产物
 * 
 * @since JDK1.8
 * @author vencano
 * @date 2017年1月8日 下午3:51:36
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration implements EnvironmentAware {
	
	/** 
	 * SpringBoot默认已经将classpath:/META-INF/resources/
	 * 和classpath:/META-INF/resources/webjars/映射
	 * 所以该方法不需要重写，如果在SpringMVC中，可能需要重写定义（我没有尝试）
	 * 重写该方法需要 extends WebMvcConfigurerAdapter * */
	
    public static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";
    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "swagger.");
    }
    
    @Bean  
    public Docket adminApi(){  
        return new Docket(DocumentationType.SWAGGER_2)  
                .groupName("Admin API")  
                .forCodeGeneration(true)  
                .pathMapping("/")  
                .select()  
                .paths(paths())  
                .build()  
                .apiInfo(apiInfo())  
                .useDefaultResponseMessages(false);  
    }  
  
    private Predicate<String> paths(){  
        return Predicates.and(PathSelectors.regex("/.*"), Predicates.not(PathSelectors.regex("/error")));  
    } 

//    @Bean
//    public Docket swaggerSpringfoxDocket() {
//        log.debug("Starting Swagger");
//        StopWatch watch = new StopWatch();
//        watch.start();
//        Docket swaggerSpringMvcPlugin = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .genericModelSubstitutes(ResponseEntity.class)
//                .select()
//                .paths(regex(DEFAULT_INCLUDE_PATTERN)) // and by paths
//                .build();
//        watch.stop();
//        log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
//        return swaggerSpringMvcPlugin;
//    }

    @SuppressWarnings("deprecation")
	private ApiInfo apiInfo() {
        return new ApiInfo(
                propertyResolver.getProperty("title"),
                propertyResolver.getProperty("description"),
                propertyResolver.getProperty("version"),
                propertyResolver.getProperty("termsOfServiceUrl"),
                propertyResolver.getProperty("contact"),
                propertyResolver.getProperty("license"),
                propertyResolver.getProperty("licenseUrl")
        );
    }   
    @SuppressWarnings("deprecation")
	public ApiInfo testApiInfo() {
        ApiInfo apiInfo = new ApiInfo("Electronic Health Record(EHR) Platform API",//大标题
                "EHR Platform's REST API, all the applications could access the Object model data via JSON.",//小标题
                "0.1",//版本
                "NO terms of service",
                "365384722@qq.com",//作者
                "The Apache License, Version 2.0",//链接显示文字
                "http://www.apache.org/licenses/LICENSE-2.0.html"//网站链接
        );

        return apiInfo;
    }
    
}